package com.schooldate.model.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.schooldate.base.BaseAction;
import com.schooldate.model.entity.MessageBoard;
import com.schooldate.model.entity.MessageBoardReply;
import com.schooldate.model.entity.UserInfo;
import com.schooldate.util.CommomUtils;
import com.schooldate.util.SimpleJsonFilter;
import com.schooldate.util.push.PushUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

/**
 * Created by etund on 2015/3/24 0024.
 */
@Controller
@Scope("prototype")
@SuppressWarnings("all")
public class MessageBoardReplyAction extends BaseAction<MessageBoardReply> {
    private String messageBoardId;
    private String replyMessageId;
    private String userId;

    /**
     * 用户点击一条留言，进去
     * @throws Exception
     */
    public void chickIntoTheMessage() throws Exception{
        Integer maxNum = 10;
        if (refreshNum!=null&&!"".equals(refreshNum.trim()))
            maxNum += Integer.parseInt(refreshNum);
        MessageBoard  messageBoard = this.messageBoardService.getById(Long.parseLong(messageBoardId));
        System.out.println(messageBoardId+"===========test==========");
        if (messageBoard!=null){
            messageBoardReplies = this.messageBoardReplyService.getByMessageBoardId(maxNum,Long.parseLong(messageBoardId));
//            if (messageBoardReplies!=null && messageBoardReplies.size()>0){
//                object.put("messageBoardReplies",messageBoardReplies);
//            }
            object.put("messageBoardReplies",messageBoardReplies);
            object.put("messageBoard",messageBoard);
            object.put("code",1);
            SimpleJsonFilter filter = new SimpleJsonFilter("chickIntoTheMessag.do");
            result = JSON.toJSONString(object,filter, SerializerFeature.WriteMapNullValue);
            renderJson(result);
            return;
        }else {
            object.put("code",0);
            result = JSON.toJSONString(object);
            renderJson(result);
            return;
        }
    }


    /**
     * 回复留言
     * @throws Exception
     */
    public void replyMessage() throws Exception{
        System.out.println(messageBoardId+userId+"=================================");
        MessageBoard messageBoard = this.messageBoardService.getById(Long.parseLong(messageBoardId));
        UserInfo userInfo = this.userInfoService.getById(Long.parseLong(userId));

        messageBoard.setCommentNum(messageBoard.getCommentNum()+1);
        this.messageBoardService.update(messageBoard);
        model.setSendTime(CommomUtils.getCurrentTime());
        model.setUserInfo(userInfo);
        model.setMbrId(model.getId());
        model.setMessageBoard(messageBoard);
        this.messageBoardReplyService.save(model);

//        消息推送
        UserInfo reciUser = this.userInfoService.getById(messageBoard.getUserInfo().getId());
        PushUtils.pushToSingle(reciUser.getDevicetoken(),"你收到一条信息新");

        object.put("code",0);
        result = JSON.toJSONString(object);
        renderJson(result);
    }

    /**
     * 回复评论
     * @throws Exception
     */
    public void replyComment() throws Exception{
        MessageBoardReply messageBoardReply = this.messageBoardReplyService.getById(Long.parseLong(replyMessageId));
        MessageBoard messageBoard = this.messageBoardService.getById(Long.parseLong(messageBoardId));
        messageBoard.setCommentNum(messageBoard.getCommentNum()+1);
        this.messageBoardService.update(messageBoard);
        UserInfo userInfo = this.userInfoService.getById(Long.parseLong(userId));
        model.setSendTime(CommomUtils.getCurrentTime());
        model.setMessageBoard(messageBoard);
        model.setUserInfo(userInfo);
        model.setMessageBoardReply(messageBoardReply);
        model.setMbrId(model.getId());
        this.messageBoardReplyService.save(model);

        //        消息推送
        UserInfo reciUser = this.userInfoService.getById(messageBoard.getUserInfo().getId());
        PushUtils.pushToSingle(reciUser.getDevicetoken(),"你收到一条信息新");

        object.put("code",0);
        result = JSON.toJSONString(object);
        renderJson(result);
    }




    public String getMessageBoardId() {
        return messageBoardId;
    }

    public void setMessageBoardId(String messageBoardId) {
        this.messageBoardId = messageBoardId;
    }

    public String getReplyMessageId() {
        return replyMessageId;
    }

    public void setReplyMessageId(String replyMessageId) {
        this.replyMessageId = replyMessageId;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }
}
